package com.liu.io;

/**
 * 正则表达式
 * \s：表示匹配任何空白字符
 * +：表示匹配一次或多次
 * ^：头
 * $：尾巴
 */
public class RegexTest {

    public static void main(String args[]) {

        String stringWithSpace ="  Hello word java  ";

        RegexTest.replaceAllTest(stringWithSpace," ");

        RegexTest.replaceAllTest(stringWithSpace,"\\s+");

        RegexTest.replaceAllTest(stringWithSpace,"^\\s+");

        RegexTest.replaceAllTest(stringWithSpace,"\\s+$");

    }


    private static void replaceAllTest(String stringWithSpace,String regex){

        System.out.println("Before replaceAll with '"+ regex +"': \'" + stringWithSpace + "\'");

        String stringAfterTrim = stringWithSpace.replaceAll(regex, "");

        System.out.println("After replaceAll with '"+ regex +"': \'" + stringAfterTrim + "\'");

        System.out.println();
    }

}
